<template>
	<view class="content fofa poRel">
		<tabnav title="我的企业联系人" :col="back"></tabnav>
		<view class="wid100 dis disAl coFFF" style="height: 120rpx;" :style="{'background-color':back}">
			<view class="textCen" style="width: 50%;" @click="xuanzeClikc(1)">
				<view :style="xuaze == 1?'color: #fff;':''">个人</view>
				<view v-if="xuaze == 1" class="mar-top10 mar" style="width: 60rpx;height: 2rpx;background-color: #fff;"></view>
				<view v-if="xuaze != 1" class="mar-top10 mar" style="width: 60rpx;height: 2rpx;"></view>
			</view>
			<view class="textCen" style="width: 50%;" @click="xuanzeClikc(2)">
				<view :style="xuaze == 2?'color: #fff;':''">企业</view>
				<view v-if="xuaze == 2" class="mar-top10 mar" style="width: 60rpx;height: 2rpx;background-color: #fff;"></view>
				<view v-if="xuaze != 2" class="mar-top10 mar" style="width: 60rpx;height: 2rpx;"></view>
			</view>
		</view>
		<view class="wid100 dis disAl bacFFF" style="height: 148rpx;">
			<view class="wid90 mar dis disAl borRad10" style="height: 71rpx;background-color: #F4F3F3;">
				<view class="dis disJuC" style="width: 13%;">
					<image :src="aurl+icon_list.sousuo" style="width: 31rpx;height: 31rpx;"></image>
				</view>
				<input class="wid87 " @input="suosouInput" placeholder="搜索联系人姓名"/>
			</view>
		</view>
		<scroll-view :style="'height: calc(100vh - 148rpx - 120rpx - 90rpx - '+tops+'px);'" 
			@refresherrefresh="onPulling" :refresher-triggered="triggered"
			scroll-y="true" class="scroll-Y" :refresher-enabled="true">
			<view class="wid100" v-if="list.length == 0">
				<view class="wid100" style="height: 220rpx;"></view>
				<view class="wid100 textCen">
					<image src="https://www.sign.run/applet/static/wenjian/wushuju.png" style="width: 306rpx;height: 200rpx;"></image>
					<view class="mar-top30 foSi25" style="color: #858585;">暂无联系人</view>
				</view>
			</view>
			<view v-if="list.length != 0 && xuaze == 1" class="wid100" v-for="(item,index) in namejson" :key="index">
				<view v-for="(item1,index1) in item" :key="index1" v-if="item1.type == 1" @click="xinjian(item1)">
					<view v-if="index1 == 0" class="foSi30 fowe600 wid95 mar dis disAl" style="height: 49rpx;">{{item1.first}}</view>
					<view class="bacFFF wid100 dis disAl" style="height: 147rpx;">
						<view class="wid87 mar ">
							<view class="foSi30">{{item1.name}}</view>
							<view class="dis disAl foSi25 mar-top20" style="color: #858484;">
								<view class="dis disAl" style="width: 35%;">
									<image src="https://www.sign.run/applet/static/wode/dianhua.png" style="width: 26rpx;height: 26rpx;"></image>
									<view class="mar-left20">{{item1.mobile}}</view>
								</view>
								<view class="dis disAl" style="width: 65%;">
									<image src="https://www.sign.run/applet/static/wode/zhengjian.png" style="width: 28rpx;height: 25rpx;"></image>
									<view class="mar-left20">{{item1.idnum==''?'证件号未记录':item1.idnum}}</view>
								</view>
							</view>
						</view>
					</view>
					<view :class="index1 == (item.length-1)?'':'xia'"></view>
				</view>
			</view>
			<view v-if="list.length != 0 && xuaze == 2" class="wid100" v-for="(item,index) in namejson" :key="index">
				<view v-for="(item1,index1) in item" :key="index1" v-if="item1.type == 2" @click="xinjian(item1)">
					<view v-if="index1 == 0" class="foSi30 fowe600 wid95 mar dis disAl" style="height: 49rpx;">{{item1.first}}</view>
					<view class="bacFFF wid100 dis disAl" style="height: 147rpx;">
						<view class="wid87 mar ">
							<view class="foSi30">{{item1.name}}</view>
							<view class="dis disAl foSi25 mar-top20" style="color: #858484;">
								<view class="dis disAl" style="width: 35%;">
									<image src="https://www.sign.run/applet/static/wode/dianhua.png" style="width: 26rpx;height: 26rpx;"></image>
									<view class="mar-left20">{{item1.mobile}}</view>
								</view>
							</view>
						</view>
					</view>
					<view :class="index1 == (item.length-1)?'':'xia'"></view>
				</view>
			</view>
			<view style="height: 140rpx" :style="{'padding-bottom': paddingBottomHeight + 'rpx'}"></view>
		</scroll-view>
		<view class="wid100 poAbs bacFFF dis disAl disJuC" :style="{'padding-bottom': paddingBottomHeight + 'rpx'}" style="height: 125rpx;bottom: 0;">
			<view class="hei80 dis disAl disJuC coFFF borRad10" @click="xinjian"
				style="width: 400rpx;" :style="{'background-color':back}">新建联系人</view>
		</view>
	</view>
</template>

<script>
	import tabnav from "../common/tabnav/tabnav.vue"
	export default {
		components: {
			tabnav,
		},
		data() {
			return {
				paddingBottomHeight: 0,
				aurl: this.global.url,
				back: uni.getStorageSync('back'),
				backTwo: uni.getStorageSync('backTwo'),
				xuaze: 1,
				suosou: '',
				// 判断是否还在继续输入
				timer: null,
				tops: 0,
				last_page: 0,
				page: 0,
				triggered: false,
				_freshing: false,
				list: [],
				firstPin :["A", "B", "C", "D", "E", "F", "G", "H", "J", "K", "L", "M", "N", "P", "Q", "R", "S", "T", "W", "X", "Y", "Z", "#"],
				namejson:{},//转换为拼音后的数据
				icon_list:{
					shaixuan:'/addon/qianyu_dzq/wechatimg/shaixuan.png',
					sousuo:'/addon/qianyu_dzq/wechatimg/sousuo.png',
					wushuju:'/addon/qianyu_dzq/wechatimg/wushuju.png',
				}
			}
		},
		onLoad() {
			this.getList()
		},
		created() {
			var that = this
			uni.getSystemInfo({
				success: (e) => {
					console.log(e)
					that.tops = e.statusBarHeight
					let model = ['X', 'XR', 'XS', '11', '12', '13', '14', '15'];
					model.forEach(item => {
						//适配iphoneX以上的底部，给tabbar一定高度的padding-bottom
						if(e.model.indexOf(item) != -1 && e.model.indexOf('iPhone') != -1) {
							that.paddingBottomHeight = 40;
						}
					})
				}
			})
		},
		methods: {
			getList() {
				let that = this
				uni.request({
					url: that.global.url+'api/qianyu_dzq/applet_person_list',
					method: 'POST',
					header:{'site-id':that.global.site_id,'token': uni.getStorageSync('inuse_userinfo_token')},
					data: {name: that.suosou},
					success(res) {
					    let result = res.data
					    if(result.code == 1){
							that.list = result.data
							that.getPinyin()
					    }else{
					        uni.showToast({
					            title: result.msg,
					            icon:'none',
								duration: 2000
					        })
					    }
					},
					complete() {
						uni.hideLoading()
					}
				})
			},
			getPinyin(){
				let nameArr = [];
				var that = this
				let pinyin = require('js-pinyin')
				pinyin.setOptions({checkPolyphone: false, charCase: 0})
				//循环数组，拿到所有的姓名
				for(var i = 0 ; i < that.list.length ; i ++){
					// 取所有姓名的首字母
					let peopleName = that.list[i].name
					// 取所有姓名的首字母
					let firstname = pinyin.getCamelChars(peopleName).substring(0, 1)
					console.log(firstname)
					var aTr = true
					for (let i = 0; i < that.firstPin.length; i++) {
						if(firstname == that.firstPin[i]){
							aTr = false
						}
					}
					if(aTr){
						//给原json添加首字母键值对
						that.list[i].first = '#'
					}else{
						//给原json添加首字母键值对
						that.list[i].first = firstname
					}
					//放入新数组
					nameArr.push(that.list[i])
					// that.result.push(res.data.content[i].id)
				}
				let namejson = {};
				//根据首字母键值对给原数据按首字母分类
				for (let i = 0; i < that.firstPin.length; i++) {    //这里的FirstPin是一个写入了所有字母的数组,见data中
					namejson[that.firstPin[i]] = nameArr.filter(function (value) {
						return value.first === that.firstPin[i]
					})
				}
				that.namejson = namejson
				console.log(that.namejson)
			},
			xuanzeClikc(index){
				this.xuaze = index
			},
			onPulling(e) {
				var that = this
				if (that._freshing) return;
				that._freshing = true;
				that.triggered = true
				console.log("onpulling", e);
				setTimeout(() => {
					that.triggered = false;
					that._freshing = false;
				}, 3000)
			},
			suosouInput: function(event) {
				if (this.timer !== null) clearTimeout(this.timer)
				this.timer = setTimeout(() => {
					this.suosou = event.target.value
					console.log(this.suosou)
					this.getList()
				}, 1000)
			},
			xinjian(list){
				if(this.xuaze == 1){
					this.tiaozhuan('/pages/wode/personageTel?list='+JSON.stringify(list))
				}else{
					this.tiaozhuan('/pages/wode/firmTel?list='+JSON.stringify(list))
				}
			},
			tiaozhuan(url){
				uni.redirectTo({
					url: url
				});
			},
		}
	}
</script>

<style>
	.content {
		height: 100vh;
		background-color: #F4F3F3;
	}
	.foSi28{
		font-size: 28rpx;
	}
	.xia{
		height: 1rpx;
		background: #BFBFBF;
	}
</style>
